home *** CD-ROM | disk | FTP | other *** search
/ PC Format (South-Africa) 2001 June / PCFJune.iso / mweb / MWEB Utils / ws295sdk.exe / Ws2sdkzp.exe / SAMPLES / LAYERED / DSOCKET.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-06-06  |  6.7 KB  |  298 lines

  1. /*++
  2.  
  3.      Copyright (c) 1996 Intel Corporation
  4.      Copyright (c) 1996 Microsoft Corporation
  5.      All Rights Reserved
  6.  
  7.      Permission is granted to use, copy and distribute this software and
  8.      its documentation for any purpose and without fee, provided, that
  9.      the above copyright notice and this statement appear in all copies.
  10.      Intel makes no representations about the suitability of this
  11.      software for any purpose.  This software is provided "AS IS."
  12.  
  13.      Intel specifically disclaims all warranties, express or implied,
  14.      and all liability, including consequential and other indirect
  15.      damages, for the use of this software, including liability for
  16.      infringement of any proprietary rights, and including the
  17.      warranties of merchantability and fitness for a particular purpose.
  18.      Intel does not assume any responsibility for any errors which may
  19.      appear in this software nor any responsibility to update it.
  20.  
  21.  
  22. Module Name:
  23.  
  24. dsocket.cpp
  25.  
  26. Abstract:
  27.  
  28. This module contains the implemetation of the dsocket object used
  29. by lsp.dll
  30.  
  31. --*/
  32.  
  33. #include "precomp.h"
  34.  
  35. LIST_ENTRY  DSOCKET::m_socket_list;
  36. CRITICAL_SECTION DSOCKET::m_socket_list_lock;
  37.  
  38. INT
  39. DSOCKET::DSocketClassInitialize (
  40.     )
  41. {
  42.     InitializeCriticalSection (&m_socket_list_lock);
  43.     InitializeListHead (&m_socket_list);
  44.     return 0;
  45. }
  46.  
  47.  
  48. INT
  49. DSOCKET::DSocketClassCleanup(
  50.     )
  51. {
  52.     PLIST_ENTRY  ListMember;
  53.     PDSOCKET     Socket;
  54.     PDPROVIDER   Provider;
  55.     SOCKET       ProviderSocket;
  56.     INT          Errno;
  57.  
  58.     //Kill all the open sockets
  59.     ListMember = m_socket_list.Flink;
  60.  
  61.     while (ListMember != &m_socket_list){
  62.         Socket = CONTAINING_RECORD(
  63.             ListMember,
  64.             DSOCKET,
  65.             m_list_linkage);
  66.         ListMember = ListMember->Flink;
  67.  
  68.         Socket->Remove ();
  69.         Provider = Socket->GetDProvider();
  70.         ProviderSocket = Socket->GetProviderSocket();
  71.  
  72.  
  73.         Provider->WSPCloseSocket(
  74.             ProviderSocket,
  75.             &Errno);
  76.  
  77.         gUpCallTable.lpWPUCloseSocketHandle(
  78.             Socket->GetSocketHandle(),
  79.             &Errno);
  80.         
  81.         delete(Socket);
  82.     } //while
  83.  
  84.     DeleteCriticalSection (&m_socket_list_lock);
  85.     return 0;
  86. }
  87.  
  88.  
  89. PDSOCKET
  90. DSOCKET::FindDSocketFromProviderSocket (
  91.     SOCKET  s
  92.     ) {
  93.     PDSOCKET    Socket = NULL;
  94.     PLIST_ENTRY ListMember;
  95.  
  96.     EnterCriticalSection (&m_socket_list_lock);
  97.     ListMember = m_socket_list.Flink;
  98.  
  99.     while (ListMember != &m_socket_list){
  100.         PDSOCKET    Socket2 = CONTAINING_RECORD(
  101.             ListMember,
  102.             DSOCKET,
  103.             m_list_linkage);
  104.         ListMember = ListMember->Flink;
  105.         if (Socket2->GetProviderSocket()==s) {
  106.             Socket = Socket2;   
  107.             break;
  108.         }
  109.     }
  110.     LeaveCriticalSection (&m_socket_list_lock);
  111.  
  112.     return Socket;
  113.  
  114. }
  115.  
  116.  
  117. DSOCKET::DSOCKET(
  118.     )
  119. /*++
  120.  
  121. Routine Description:
  122.  
  123.     DSOCKET  object  constructor.   Creates and returns a DSOCKET object.  Note
  124.     that  the  DSOCKET object has not been fully initialized.  The "Initialize"
  125.     member function must be the first member function called on the new DSOCKET
  126.     object.
  127.  
  128. Arguments:
  129.  
  130.     None
  131.  
  132. Return Value:
  133.  
  134.     None
  135. --*/
  136. {
  137.     // Set our data members to known values
  138.     m_provider          = NULL;
  139.     m_socket_handle     = INVALID_SOCKET;
  140.     m_catalog_entry_id  = NULL;
  141.     m_provider_socket   = INVALID_SOCKET;
  142.     m_completion_context = INVALID_SOCKET;
  143.     m_async_events      = NULL;
  144.     m_async_window      = NULL;
  145.     m_async_message     = NULL;
  146.     m_closing            = FALSE;
  147. }
  148.  
  149.  
  150.  
  151.  
  152. INT
  153. DSOCKET::Initialize(
  154.         IN PDPROVIDER Provider,
  155.         IN SOCKET     ProviderSocket,
  156.         IN DWORD      CatalogEntryId,
  157.         IN SOCKET     SocketHandle
  158.         )
  159. /*++
  160.  
  161. Routine Description:
  162.  
  163.     Completes  the  initialization  of  the  DSOCKET object.  This must be the
  164.     first  member  function  called  for  the  DSOCKET object.  This procedure
  165.     should be called only once for the object.
  166.  
  167. Arguments:
  168.  
  169.     Provider - Supplies  a  reference  to  the DPROVIDER object associated with
  170.                this DSOCKET object.
  171.  
  172.     ProviderSocket - The socket handle returned from the lower level provider.
  173.  
  174.     CatalogEntryId - The CatalogEntryId for the provider referenced by
  175.                      m_provider.
  176.  
  177.     SocketHandle        - The socket handle returned from WPUCreateSocketHandle().
  178.  
  179. Return Value:
  180.  
  181.     The  function returns ERROR_SUCCESS if successful.  Otherwise it
  182.     returns an appropriate WinSock error code if the initialization
  183.     cannot be completed.
  184. --*/
  185. {
  186.     // Store the provider and process object.
  187.     m_provider = Provider;
  188.     m_provider_socket = ProviderSocket;
  189.     m_catalog_entry_id = CatalogEntryId;
  190.     m_socket_handle = SocketHandle;
  191.  
  192.     // Add this socket to the list of sockets.
  193.     EnterCriticalSection(&m_socket_list_lock);
  194.     InsertHeadList(
  195.         &m_socket_list,
  196.         &m_list_linkage);
  197.     LeaveCriticalSection(&m_socket_list_lock);
  198.     DEBUGF( DBG_TRACE,
  199.             ("Initializing socket %X\n",this));
  200.     return(ERROR_SUCCESS);
  201. }
  202.  
  203. VOID
  204. DSOCKET::Remove (
  205.     )
  206. {
  207.     EnterCriticalSection(&m_socket_list_lock);
  208.     RemoveEntryList(&m_list_linkage);
  209.     LeaveCriticalSection(&m_socket_list_lock);
  210. }
  211.  
  212.  
  213. DSOCKET::~DSOCKET()
  214. /*++
  215.  
  216. Routine Description:
  217.  
  218.     DSOCKET  object  destructor.   This  procedure  has  the  responsibility to
  219.     perform  any required shutdown operations for the DSOCKET object before the
  220.     object  memory  is  deallocated.
  221.  
  222. Arguments:
  223.  
  224.     None
  225.  
  226. Return Value:
  227.  
  228.     None
  229. --*/
  230. {
  231.  
  232.     DEBUGF( DBG_TRACE,
  233.             ("Destroying socket %X\n",this));
  234. }
  235.  
  236.  
  237. VOID
  238. DSOCKET::RegisterAsyncOperation(
  239.     HWND     Window,
  240.     UINT     Message,
  241.     LONG     Events
  242.     )
  243. /*++
  244.  
  245. Routine Description:
  246.  
  247.     Registers interest in net work events.
  248.  
  249. Arguments:
  250.  
  251.     Window  - The handle to the window that will receive notification of
  252.               network events.
  253.  
  254.     Message - The message to send for net event notification.
  255.  
  256.     Events  - The events to be registered.
  257.  
  258. Return Value:
  259.  
  260.     NO_ERROR on success else a valid winsock errorcode.
  261.  
  262. --*/
  263. {
  264.  
  265.     m_async_window  = Window;
  266.     m_async_message = Message;
  267.     m_async_events  = Events;
  268. }
  269.  
  270. VOID
  271. DSOCKET::SignalAsyncEvents(
  272.     LPARAM    lParam
  273.     )
  274. /*++
  275.  
  276. Routine Description:
  277.  
  278.      The notification function called by the worker thread to signal network
  279.      events.
  280.  
  281. Arguments:
  282.  
  283.     None
  284.  
  285. Return Value:
  286.  
  287.     None
  288. --*/
  289. {
  290.  
  291.     gUpCallTable.lpWPUPostMessage (m_async_window,
  292.                                     m_async_message,
  293.                                     (WPARAM)m_socket_handle,
  294.                                     lParam);
  295.  
  296. }
  297.  
  298.